/*
 * Copyright 2010 Yannick Stucki (yannickstucki.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.yannickstucki.android.musicqueue.playback;

import java.io.IOException;

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;

import com.yannickstucki.android.musicqueue.communication.IMusicPlayer;
import com.yannickstucki.android.musicqueue.communication.INotificator;
import com.yannickstucki.android.musicqueue.communication.OnSongCompletedListener;
import com.yannickstucki.android.musicqueue.data.Song;
import com.yannickstucki.android.musicqueue.util.Logger;

/**
 * A wrapper around the android provided media player.
 * 
 * @author Yannick Stucki
 * 
 */
public class MusicPlayer extends MediaPlayer implements IMusicPlayer, OnCompletionListener,
    OnPreparedListener, OnErrorListener {

  /**
   * A listener to notify whenever a song has completed playing.
   */
  private OnSongCompletedListener onSongCompletedListener;

  /**
   * An interface to create status bar notifications.
   */
  private INotificator notificator;

  /**
   * The playing song.
   */
  private Song song;

  /**
   * A flag to determine whether the next song should be playing as well.
   */
  private boolean continuePlaying;

  /**
   * The constructor needs an INotificator.
   */
  public MusicPlayer(INotificator notificator) {
    this.notificator = notificator;
    this.setOnCompletionListener(this);
    setOnPreparedListener(this);
  }

  @Override
  public void pause() {
    super.pause();
    notificator.cancelNotification();
  }

  @Override
  public void play() {
    if (!isPlaying()) {
      start();
      notificator.showNotification(song);
    }
  }

  @Override
  public void setOnSongCompletedListener(OnSongCompletedListener onSongCompletedListener) {
    this.onSongCompletedListener = onSongCompletedListener;
  }

  @Override
  public void onCompletion(MediaPlayer mp) {
    onSongCompletedListener.onSongCompleted();
    Logger.out("on completion");
  }

  @Override
  public void setSongAndPlay(Song song) {
    continuePlaying = true;
    setSongHelper(song);
  }

  @Override
  public void setSong(Song song) {
    continuePlaying = isPlaying();
    setSongHelper(song);
  }

  /**
   * Internal method used to actually set a song because the public interface
   * offers two options.
   */
  private void setSongHelper(Song song) {
    this.song = song;
    reset();
    Logger.out("set song");
    try {
      setDataSource(song.path);
      prepareAsync();
    } catch (IllegalArgumentException e) {
      Logger.debug(e);
    } catch (IllegalStateException e) {
      Logger.debug(e);
    } catch (IOException e) {
      Logger.debug(e);
    }
  }

  @Override
  public void destroy() {
    notificator.cancelNotification();
    stop();
    release();
  }

  @Override
  public void onPrepared(MediaPlayer mp) {
    if (continuePlaying) {
      play();
    }

    Logger.debug("started " + song.title);
  }

  @Override
  public boolean onError(MediaPlayer mp, int what, int extra) {
    Logger.debug("ERROR IN PLAYER: " + what + " " + extra);
    return false;
  }
}
